#define  _CRT_SECURE_NO_WARNINGS 1

int treeSize(struct TreeNode* root)
{
    return root == NULL ? 0 : treeSize(root->left) + treeSize(root->right) + 1;
}

void prevOrder(struct TreeNode* root, int* a, int* pi)
{
    if (root == NULL)
    {
        return;
    }

    a[(*pi)++] = root->val;
    prevOrder(root->left, a, pi);
    prevOrder(root->right, a, pi);
}

int* preorderTraversal(struct TreeNode* root, int* returnSize) {
    *returnSize = treeSize(root);
    int* a = (int*)malloc(sizeof(int) * (*returnSize));

    int i = 0;
    prevOrder(root, a, &i);

    return a;
}